package io.renren.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.HttpResult;
import io.renren.common.utils.HttpUtils;
import io.renren.util.MD5;
import io.renren.util.RandomUtils;
import io.renren.util.SignUtils;

import java.util.HashMap;
import java.util.Set;

/**
 * @author lizhi
 * @Date:
 */
public class JiuxiaoCreateOrder {
    public static void main(String[] args) throws Exception{

        JSONObject reqdata=new JSONObject(true);
     //   WXH5


        String userName= "U智付";
        String securityKey= "6ed580eae7bb4a41a626b68859ccd3ba";
        String partnerOrderId= RandomUtils.generateLowerString(20);
        String description= "女装";
        String amount= "30000";
        String channelCode= "T_PDD_AP";
        String notifyUrl= "http://127.0.0.1:8090/api/xwcallback";
        String secretkey= "f83ad24c41ac46998574f6d241b32e87";


        //支付宝
        reqdata.put("userName", userName);
        reqdata.put("securityKey",securityKey);
        reqdata.put("partnerOrderId", partnerOrderId);
        reqdata.put("notifyUrl","http://127.0.0.1:8090/api/xwcallback");
        reqdata.put("description",description);
        reqdata.put("amount",amount);
        reqdata.put("channelCode",channelCode);

        String tmp = userName + securityKey + partnerOrderId + description + amount + channelCode + notifyUrl + secretkey;
        System.out.println("待加密： "+tmp);
        String sign = MD5.threeMd5(tmp);
        String apiurl = "http://lucky666888.com/bxpay/export/bill/create";




        reqdata.put("sign",sign);

        System.err.println("商户请求参数:"+reqdata);

        HttpResult result= HttpUtils.doPostJson(apiurl, reqdata.toJSONString());
        System.out.println("result:"+result);
         if(200==result.getCode()){
            System.err.println("订单创建返回参数:"+ result.getBody());
           String body = result.getBody();
           if(body.contains("pay_url")){
               JSONObject resobj= JSON.parseObject(result.getBody());
               String qr_url = resobj.getString("pay_url");
           }




        }else{

            System.err.println("创建订单失败"+result.getCode()+"---"+result.getBody());
        }
    }


    public static String decodeUnicode(final String dataStr) {
        int start = 0;
        int end = 0;
        final StringBuffer buffer = new StringBuffer();
        while (start > -1) {
            end = dataStr.indexOf("\\u", start + 2);
            String charStr = "";
            if (end == -1) {
                charStr = dataStr.substring(start + 2, dataStr.length());
            } else {
                charStr = dataStr.substring(start + 2, end);
            }
            char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
            buffer.append(new Character(letter).toString());
            start = end;
        }
        return buffer.toString();
    }
}
